package com.qingyun.cameralab.preview_capture

import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import com.google.android.material.snackbar.Snackbar
import com.qingyun.cameralab.EmptyActivity
import com.qingyun.cameralab.R
import com.qingyun.cameralab.databinding.ActivityPreviewCaptureBinding

class PreviewCaptureActivity : AppCompatActivity(), CameraPreviewManager.PermissionCallback {
    companion object {
        private val REQUEST_CODE_PERMISSIONS = 10
    }

    private lateinit var binding: ActivityPreviewCaptureBinding
    private lateinit var cameraManager: CameraPreviewManager
    private lateinit var captureHelper: PreviewCaptureHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPreviewCaptureBinding.inflate(layoutInflater)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        cameraManager = CameraPreviewManager.Builder(this)
            .setPreviewView(binding.previewView)
            .setLifecycleObserver(this)
            .setLensFacing(CameraSelector.LENS_FACING_FRONT)
            .setPermissionCallback(this)
            .build()
        captureHelper = PreviewCaptureHelper.Builder(this)
            .setRootView(binding.contentView)
            .setPreviewView(binding.previewView)
            .setOutputQuality(90)
            .setOutputFormat(Bitmap.CompressFormat.JPEG)
            .setBackgroundColor(Color.WHITE)
            .setCaptureCallback(object : PreviewCaptureHelper.CaptureCallback {
                override fun onCaptureStart() {
                    Toast.makeText(this@PreviewCaptureActivity, "开始截图", Toast.LENGTH_SHORT)
                        .show()
                }

                override fun onCaptureSuccess(result: PreviewCaptureHelper.CaptureResult) {
                    result.bitmap?.let {
                        binding.ivBitmap.setImageBitmap(it)
                        binding.ivBitmap.isVisible = true
                    } ?: run {
                        Snackbar.make(binding.root, "获取图片失败!", Snackbar.LENGTH_SHORT).show()
                    }
                }

                override fun onCaptureError(e: Exception) {
                    e.printStackTrace()
                    Toast.makeText(this@PreviewCaptureActivity, "截图异常", Toast.LENGTH_SHORT)
                        .show()

                }

            })
            .build()

        binding.btnCapture.setOnClickListener {
            captureHelper.capture()
        }
        binding.btnGone.setOnClickListener {
            binding.ivBitmap.setImageBitmap(null)
            binding.ivBitmap.isVisible = false
        }
        binding.tvSignature.setOnClickListener {
            EmptyActivity.start(this)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray,
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE_PERMISSIONS) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                cameraManager.startPreview()
            }
        }
    }

    override fun onPermissionRequired() {
        PermissionHelper.requestCameraPermission(this, REQUEST_CODE_PERMISSIONS)
    }
}